<template>
  <div class="xtx-checkbox" @click="toggle">
    <i
      class="iconfont"
      :class="checked ? 'icon-checked' : 'icon-unchecked'"
    ></i>
    <span><slot/></span>
  </div>
</template>

<script>
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    const checked = useVModel(props, 'modelValue', emit)

    const toggle = () => {
      // 点击取反  --- temp中间变量是处理登录页面复选框校验规则的
      const temp = !checked.value
      checked.value = temp
      // 模拟表单内绑定change事件
      emit('change', temp)
    }

    return { checked, toggle }
  }
}
</script>

<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
